package models

type DockerImage struct {
	//数据库ID非docker image ID
	ID      string `json:"id" gorm:"primary_key;column:image_id"`
	Name    string `json:"name" gorm:"unique"`
	Size    int64  `json:"size"`
	Version string `json:"version"`

	//docker 存储的image ID
	DockerImageID string `json:"docker_image_id"`
	BaseModel
}
/*
func (img *DockerImage) Get(imageId string) (DockerImage, error) {
	var dockerImage DockerImage
	err := Db.Where("image_id = ?", imageId).First(&dockerImage).Error

	if err != nil {
		return dockerImage, err
	}

	return dockerImage, nil
}

func (img *DockerImage) GetDockerImages() ([]*DockerImage, error) {
	var imgs []*DockerImage

	err := Db.Find(&imgs).Error

	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	return imgs, nil

}

func (img *DockerImage) Delete(imageId string) error {
	var dockerImage DockerImage
	err := Db.Where("image_id = ?", imageId).Delete(&dockerImage).Error

	return err
}

func (img *DockerImage) Create() (DockerImage, error) {
	//判断img是否存在
	var count int

	Db.Model(DockerImage{}).Where("name = ?&& size = ?", img.Name, img.Size).Count(&count)
	if count > 1 {
		return DockerImage{}, errors.New(fmt.Sprintf("image: %s already exsist", img.Name))
	}

	//插入数据，并返回。
	img.ID = utils.GenUUID()
	if err := Db.Create(&img).Error; err != nil {
		return DockerImage{}, err
	}
	return *img, nil
}
*/